import 'dart:convert';

import 'package:flutter/services.dart';

class AssetsLoadUtil {
  static Map<String, dynamic> manifestMap = {};

  static Future<void> init() async {
    if (manifestMap.isEmpty) {
      String manifestContent = await rootBundle.loadString('AssetManifest.json');
      manifestMap = json.decode(manifestContent);
    }
  }

  static List<String> getFileList(String rootPath) {
    List<String> imageNames = manifestMap.keys.where((String key) => key.contains(rootPath)).toList();
    List<String> list = [];
    for (String item in imageNames) {
      list.add(item.replaceAll(rootPath, ""));
    }
    return list;
  }

  ///读取assets文件
  static Future<Uint8List> readFile(String path) async {
    //内置文件
    var data = await rootBundle.load(path);
    return data.buffer.asUint8List();
  }
}
